<?php
class Contact
{
    public $mailer = '';
    private $time_spam = 300; // 300sec = 5 min

    
    public function __construct($paramClass, $mailer = '', $spam_duree = '')
    {
        $this->mailer = $mailer;
        if($spam_duree != '' && is_numeric($spam_duree))
            $this->time_spam = $spam_duree;
    }
    
    public function send()
    {
        global $lang;
        
        // RECUPERATION DONNEE
        if(!$nom = retrieve('POST','nom', true)) redirect('contact.php?error=nom');
    	if(!$email = retrieve('POST','email',true)) redirect('contact.php?error=email');
    	if(!$sujet = retrieve('POST','sujet',true)) redirect('contact.php?error=sujet');
    	if(!$message = retrieve('POST','message', true)) redirect('contact.php?error=message');
    	if($nom == '' || $email == '' || $sujet == '' || $message == '') redirect('contact.php?error=multi');
    	if(!Mailer::validate_email($email)) redirect('contact.php?error=email');                                     // Verification synthaxe
    	if(!$this->mailer->validate_email_mx($email,$errstr)) redirect('contact.php?error=email');                   // validation dns email
    	$prenom = retrieve('POST','prenom');
    	$telephone = retrieve('POST','telephone');
    	$agence = retrieve('POST','agence');
        $annonce = retrieve('POST','annonce');
        
        if($this->already_send($sujet, $message)) redirect('contact.php?error=email_deja_envoye');                  // Verification message deja envoyer
        if($this->check_spam()) redirect('contact.php?error=please_wait');                                          // Verification spam
        
        // ENREGISTREMENT BASE
        $ArrayQuery = array(
    	'nom'	=>	addslashes($nom),
    	'prenom'	=>	addslashes($prenom),
    	'email' =>	addslashes($email),
    	'telephone'	=>	addslashes($telephone),
    	'agence'	=>	addslashes($agence),
    	'sujet'	=>	addslashes($sujet),
    	'message'	=>	addslashes($message),
    	'date'	=>	time(),
        'session' => $_SESSION['session_id'],
        'annonce'   =>  $annonce,
    	'ip'	=>	base64_encode($_SERVER['REMOTE_ADDR']));
    	
    	$GLOBALS['db']->execute(CreateQueryInsert($ArrayQuery, 'immo_contact')); 
        $email_send = $this->get_email($agence); 
        
        $this->mailer->set_from($email, $nom . ' '. $prenom);
    	$this->mailer->set_address($email_send);
    	$this->mailer->set_subject($sujet);
    	$this->mailer->set_message($message);
        
    	if(!$this->mailer->send())
        {
            $GLOBALS['smarty']->assign('error_msg', true);
            $GLOBALS['smarty']->assign('error_class', 'error_warning');
            $GLOBALS['smarty']->assign('error_image', 'comment_warning');
            $GLOBALS['smarty']->assign('error_message',$lang['Erreur_envoye_email']);
            $GLOBALS['smarty']->assign('url_site',$GLOBALS['setting']['url_site']);   
        }
        else
        {
            $GLOBALS['smarty']->assign('error_msg', true);
            $GLOBALS['smarty']->assign('error_class', 'error_success');
            $GLOBALS['smarty']->assign('error_image', 'comment_ok');
            $GLOBALS['smarty']->assign('error_message',$lang['Email_envoye']);
            $GLOBALS['smarty']->assign('url_site',$GLOBALS['setting']['url_site']);    
        }                   
    }
    
    private function already_send($sujet, $message)
    {
        if($GLOBALS['db']->sql_nbresultat(null, "SELECT COUNT(id) FROM immo_contact WHERE sujet = '". addslashes($sujet) ."' AND message = '". addslashes($message) ."'") == 0)
            return false;
        else
            return true;
    }
    
    private function check_spam()
    {
        $t = time() - 300;
        if($GLOBALS['db']->sql_nbresultat(null, "SELECT COUNT(id) FROM immo_contact WHERE session = '". $_SESSION['session_id'] ."' AND date > '". $t ."'") == 0)
            return false;
        else
            return true;                
    }
    
    private function get_email($agence)
    {
        if($agence == 0) 
            $email_send = $GLOBALS['setting']['email_contact'];
    	else
    	{
            $A = $GLOBALS['db']->query("SELECT email FROM immo_agence WHERE id = '". $agence ."'", 'BOTH');
            if($A['email'] != '')
                $email_send = $A['email'];
            else
                $email_send = $GLOBALS['setting']['email_contact']; 
        }

        return $email_send;
    }
}
?>